package xunmeng.web.servlet;

import xunmeng.domain.User;
import xunmeng.service.UserService;
import xunmeng.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addUserServlet")
public class addUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //对添加学生表单传过来的值进行对象封装
        req.setCharacterEncoding("UTF-8");//设置请求编码集
        String name=req.getParameter("name");
        String gender=req.getParameter("gender");
        String age=req.getParameter("age");
        String address=req.getParameter("address");
        String qq=req.getParameter("qq");
        String email=req.getParameter("email");
        User user=new User();
        user.setName(name);
        user.setAddress(address);
        user.setGender(gender);
        user.setAge(Integer.valueOf(age));
        user.setQq(qq);
        user.setEmail(email);

        //封装好对象后将对象传给service层进行操作
        UserService userService=new UserServiceImpl();
        if (userService.addUser(user)){
            resp.sendRedirect(req.getContextPath()+"/userlistservlet");
        }
//        System.out.println(name+";"+gender+";"+age+";"+address+";"+qq+email);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
